package com.herench.bs.transfer;

import com.herench.bs.transfer.Dialect;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cleverClinic
 * com.herench.bs.transfer.transfer
 *
 * @author wangwenkai
 * @date 2017/11/8/13:12
 */
public class SaveclinicChargeDataInsuranceTransfer extends AbstractTransfer {

    public SaveclinicChargeDataInsuranceTransfer(String method, Client client,String messageMethod) {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String paySeq = (String) params.get("paySeq");
        String idCard = (String) params.get("idCard");
        String resDate = (String) params.get("resDate");
        String patientId = (String) params.get("patientId");
        String transactNo = (String) params.get("transactNo");
        String visitNo = (String) params.get("visitNo");
        String insurNo = (String) params.get("insurNo");
        String payType = (String) params.get("payType");
        String insurVisitId = (String) params.get("insurVisitId");
        String insuranceType = (String) params.get("insuranceType");
        String mtType = (String) params.get("mtType");
        String rcptNo = (String) params.get("rcptNo");
//        String syjyxm = (String) params.get("syjyxm");//合并去掉

//        String payType = (String) params.get("payType");
        //微信21；支付宝20；银联19
//        if ("1".equals(payType)) {
//            payType = "21";
//        } else if ("6".equals(payType)) {
//            payType = "20";
//        } else if ("8".equals(payType)) {
//            payType = "19";
//        }
        List<Map<String, Object>> costList = (List<Map<String, Object>>) params.get("costList");
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElementRegist = tplDoc.getRootElement().element("CommitData").addElement("Regist").addElement("DataRow");
        dataRowElementRegist.addAttribute("PERSON_NO", idCard).addAttribute("PATIENT_ID", patientId).addAttribute("VISIT_NO", visitNo).addAttribute("VISIT_DATE", resDate).addAttribute("RCPT_NO",rcptNo).addAttribute("TRAN_NO", transactNo);
        Element dataElementOrder = tplDoc.getRootElement().element("CommitData").addElement("Order");
        for (Map<String, Object> subCostMap : costList) {
            String clinicCostItemNo = (String) subCostMap.get("clinicCostItemId");
            String itemCode = (String) subCostMap.get("itemCode");
            String itemName = (String) subCostMap.get("itemName");
            String charges = (String) subCostMap.get("charges");
            Element dataRowOrder =  dataElementOrder.addElement("DataRow");
            dataRowOrder.addAttribute("CLINIC_COST_ITEM_NO", clinicCostItemNo).addAttribute("ITEM_CODE", itemCode).addAttribute("ITEM_NAME", itemName).addAttribute("CHARGES", charges).addAttribute("APP_NO", paySeq);
        }
        Element dataRowElementInsurance = tplDoc.getRootElement().element("CommitData").addElement("Insurance").addElement("DataRow");
        dataRowElementInsurance.addAttribute("INSUR_NO", insurNo).addAttribute("INSUR_PWD", "").addAttribute("INSUR_VISIT_ID", insurVisitId).addAttribute("INSURANCE_TYPE", insuranceType).addAttribute("PAY_TYPE", payType).addAttribute("MT_TYPE", mtType);
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            Element dataRowBill = document.getRootElement().element("ReturnData").element("Bill").element("DataRow");
            SaveclinicChargeDataTransferInnerMessage innerMessageBill = new SaveclinicChargeDataTransferInnerMessage(dataRowBill);
            Element dataRowInsurance = document.getRootElement().element("ReturnData").element("Insurance").element("DataRow");
            SaveclinicChargeDataTransferInnerMessage innerMessageInsurance = new SaveclinicChargeDataTransferInnerMessage(dataRowInsurance);
            Element dataRowPayment = document.getRootElement().element("ReturnData").element("Payment").element("DataRow");
            SaveclinicChargeDataTransferInnerMessage innerMessagePayment = new SaveclinicChargeDataTransferInnerMessage(dataRowPayment);
            resMap.put("rcptNo", innerMessageBill.getRcptNo());
            resMap.put("deptLocation", "");
            resMap.put("insurPay", innerMessageInsurance.getInsurPay());
            resMap.put("accountPay", innerMessageInsurance.getAccountPay());
            resMap.put("ownPay", innerMessageInsurance.getOwnPay());
            resMap.put("tranNo", innerMessagePayment.getTranNo());
            resMap.put("code", executeStatus);
            resMap.put("message", "成功！");
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }


    public class SaveclinicChargeDataTransferInnerMessage implements InnerMessage {
        /**
         * 内部票据号
         */
        private String rcptNo;
        /**
         * 科室位置
         */
        private String deptLocation;
        /**
         * 统筹支付
         */
        private String insurPay;
        /**
         * 个人账户支付
         */
        private String accountPay;
        /**
         * 个人自费
         */
        private String ownPay;
        /**
         * 合同号
         */
        private String tranNo;

        public String getRcptNo() {
            return rcptNo;
        }

        public String getDeptLocation() {
            return deptLocation;
        }

        public String getInsurPay() {
            return insurPay;
        }

        public String getAccountPay() {
            return accountPay;
        }

        public String getOwnPay() {
            return ownPay;
        }

        public String getTranNo() {
            return tranNo;
        }

        public SaveclinicChargeDataTransferInnerMessage(Element element) {
            this.rcptNo = element.attributeValue("TOTAL_RCPT_NO");
            this.deptLocation = element.attributeValue("DEPT_LOCATION");
            this.insurPay = element.attributeValue("INSUR_PAY");
            this.accountPay = element.attributeValue("ACCOUNT_PAY");
            this.ownPay = element.attributeValue("OWN_PAY");
            this.tranNo = element.attributeValue("TRAN_NO");
        }
    }


}
